#include<fstream>
#include <ios>
#include<iostream>
#include <ostream>
#include <string>
#include "contact.pb.h"
using namespace std;

void AddPeopleInfo(contact::PeopleInfo* people)
{
   cout << "------------------新增联系人--------------------" << endl;
   string name;
   cout << "请输入联系人姓名: ";
   getline(cin,name);
   people->set_name(name);

   cout << "请输入联系人年龄: ";
   int age;
   cin >> age;
   people->set_age(age);
   cin.ignore(256,'\n');

   for(int i = 0 ; ;i++)
   {
     cout << "请输入联系人电话: " << i+1 << "(只输入回车完成电话新增): " ;
     string number;
     getline(cin,number);
     if(number.empty()) break;
     contact::PeopleInfo_Phone* phone = people->add_phone();
     phone->set_number(number);
     cout << "请输入该电话类型(1. 移动电话 2. 固定电话): ";
     int type;
     cin >> type;
     cin.ignore(256,'\n');
     switch (type) 
     {
       case 1:
          phone->set_type(contact::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
          break;
       case 2:
          phone->set_type(contact::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
          break;
       default:
          cout << "输入错误!" << endl;
          break;    
     }
   }
   contact::Address address;
   string home_address ;
   cout << "请输入联系人家庭地址:" ;
   getline(cin,home_address);
   address.set_home_address(home_address);
   string unit_address;
   cout <<"请输入联系人单位地址: " ;
   getline(cin,unit_address);
   address.set_unit_address(unit_address);
   //message->Any
   people->mutable_data()->PackFrom(address);
    
   //
   cout << "请输入联系人的其他联系方式: (1.qq 2.wechat)";
   int other_type;
   cin >> other_type;
   cin.ignore(256,'\n');
   string other_chat;
   if(other_type == 1)
   {
     cout << "请输入qq:" ;
     getline(cin,other_chat);
     people->set_qq(other_chat);
   }
   else if(other_type == 2)
   {
     cout << "请输入微信:" ;
     getline(cin,other_chat);
     people->set_wechat(other_chat);
   }
   else 
   {
      cout << "输入错误!" << endl;
   }

   for(int i = 0 ; ;i++)
   {
        cout << " 请输入备注" << i+1<<"标题(只输入回车完成备注新增):";
        string remark_key;
        getline(cin,remark_key);
        if(remark_key.empty()) break;

        cout << "请输入备注" << i+1 << "内容:";
        string remark_val;
        getline(cin,remark_val);
        people->mutable_remark()->insert({remark_key,remark_val});
   }
   cout << "------------------新增联系人成功--------------------" << endl;
   
}

int main()
{
    //1.读取本地已经存在的通讯录文件
    contact::Contacts contacts;
    fstream in("contact.bin",ios::binary | ios::in);
    if(in.is_open() == false)
    {
        cout << "打开通讯录文件失败!" << endl;
        return -1;
    }
    else if(!contacts.ParseFromIstream(&in))
    {
         cout << "parse error!" << endl;
         in.close();
         return -1;
    }
    //2.向通讯录新增一个联系人
    AddPeopleInfo(contacts.add_contacts());
    //3.将通讯录写入本地文件中
    fstream out("contact.bin",ios::binary|ios::out|ios::trunc);
    if(out.is_open() == false)
    {
           cout << "打开通讯录文件失败!" << endl;
           in.close();
           return -1;
    }
    else if(!contacts.SerializeToOstream(&out))
    {
        cout << "serialize error" << endl;
        in.close();
        out.close();
        return -1;
    }
    cout << "success !" << endl;
    in.close(); 
    out.close();
    return 0;
}